/*
 * DSP Example is part of the DSP Lecture at TEC-Costa Rica
 * Copyright (C) 2010  Pablo Alvarado
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file   mainwindow.h
 *         Implements the equalizer H(w) computation
 * \author Pablo Alvarado
 * \date   2010.12.12
 *
 * $Id: mainwindow.h $
 */

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include <QFileDialog>
#include <QTextStream>
#include <qwt_plot.h>
#include "bodeplot.h"

#include "dspsystem.h"
#include "iirfiltdialog.h"
#include "bodeplot.h"

class QwtPlotZoomer;
class QwtPlotPicker;
class QwtPlotPanner;
class Plot;
class QPolygon;

namespace Ui {
class MainWindow;
}

/**
 * Main Window Class
 *
 * This class implements Qt's main window concept.
 */
class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    QWidget *boplot;
    QString vPos;

    enum {
        /**
     * Number of bands in the equalizer
     */
        NumBands=16,

        /**
     * Number of samples for the FFT
     */
        FFTSize=1024
    };

    /**
   * Default explicit constructor.
   */
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    /**
   * Read the values on the real equalizer class an set the slider
   * values accordingly.
   */
    void updateEqualizer();

    /**
   * Read the values on the real reverberator class an set the controls
   * accordingly.
   */
    void updateReverb();

private:
    Ui::MainWindow *ui;

    /**
   * Tolerance value
   */
    static const float Epsilon;

    /**
   * Verbose flag30
   */
    bool verbose_;

    /**
   * Timer used to recompute the filter once the user changes the
   * values
   */
    QTimer *timer_;

    /**
   * List of selected files so far
   */
    QStringList selectedFiles_;

    /**
   * Equalization changed
   */
    bool eqChanged_;

    /**
   * Pointer to an inherited class of processor, which does
   * all the real work.
   */
    dspSystem* dsp_;

    /**
   * Convert a band value to the slider range
   */
    int bandToSlider(const float band) const;

    /**
   * Convert a slider value to the band range
   */
    float sliderToBand(const int val) const;

    /**
   * Summarizes the task when an equalizer slider is being moved
   */
    void setFrequencySlider(const int idx, const int value);

private Q_SLOTS:
    void moved(const QPoint &);
    void selected(const QPolygon &);
    void enableZoomMode(bool);

    /** FIR AND IIR Filter  */
private slots:

    void on_delaySlider_valueChanged(int value);
    void on_resetButton_clicked();
    void on_presetsBox_currentIndexChanged(int index);
    void on_filter60_toggled(bool checked);
    void on_equalizer_toggled(bool checked);
    void on_reverb_toggled(bool checked);
    void on_fileEdit_returnPressed();
    void on_fileButton_clicked();
    void on_dialAlpha_dialMoved(int value);
    void on_alphaSpinBox_valueChanged(double );

    void on_freq01Slider_valueChanged(int value);
    void on_freq02Slider_valueChanged(int value);
    void on_freq03Slider_valueChanged(int value);
    void on_freq04Slider_valueChanged(int value);
    void on_freq05Slider_valueChanged(int value);
    void on_freq06Slider_valueChanged(int value);
    void on_freq07Slider_valueChanged(int value);
    void on_freq08Slider_valueChanged(int value);
    void on_freq09Slider_valueChanged(int value);
    void on_freq10Slider_valueChanged(int value);
    void on_freq11Slider_valueChanged(int value);
    void on_freq12Slider_valueChanged(int value);
    void on_freq13Slider_valueChanged(int value);
    void on_freq14Slider_valueChanged(int value);
    void on_freq15Slider_valueChanged(int value);
    void on_freq16Slider_valueChanged(int value);

    void update();
    void on_exitBut_clicked();
    void on_playBut_clicked();
    void on_stopBut_clicked();
    void on_volumeSlider_valueChanged(int value);
    void on_iirfilter_toggled(bool checked);
    void on_firfilter_toggled(bool checked);
    void on_resetIIRvalues_pressed();
    void on_resetFIRvalues_pressed();
    void on_showfreq_clicked();
    void on_spNumCoefs_valueChanged(int arg1);
    void on_spFreqC1_valueChanged(int arg1);
    void on_spFreqC2_valueChanged(int arg1);
    void on_dspAtenuation_valueChanged(double arg1);
    void on_spFilterType_valueChanged(int arg1);
    void on_spFilterWindowType_valueChanged(int arg1);
    void on_spFIlterOrder_IIR_valueChanged(int arg1);
    void on_spCutFreq1_IIR_valueChanged(int arg1);

    void on_cb_lowpass_toggled(bool checked);
    void on_cb_highpass_toggled(bool checked);
    void on_spCutFreq2_IIR_valueChanged(int arg1);

    void on_pushButton_clicked();

private:
    void showInfo(QString text = QString::null);
    Plot *d_plot;
    QwtPlotZoomer *d_zoomer[2];
    QwtPlotPicker *d_picker;
    QwtPlotPanner *d_panner;
};

#endif // MAINWINDOW_H
